﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Drawing;

namespace WebProxy
{
    public static class ThumbnailManager
    {
        //public static string ImgFolderName = Server.MapPath("~/ImgCache/");

        public static string GetThumbnailForUrl(string oryginalUrl, string ImgFolderPath, ThumbnailSizes ThumbnailSize)
        {
            string ThumbnailID = Utility.Crypto.MD5.ComputeMD5Hash(oryginalUrl);

            if (File.Exists(ImgFolderPath + Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_" + ThumbnailSize.ToString() + ".jpg"))
            {
                return Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_" + ThumbnailSize.ToString() + ".jpg";
            }
            return string.Empty;
        }

        public static bool SaveThumbnailFromUrlForAllSizes(string url, string ImgFolderPath)
        {
            //sprawdz czy juz istnieje
            if (GetThumbnailForUrl(url, ImgFolderPath, ThumbnailSizes.HighDpi) != string.Empty)
            {
                //nie istnieje - stworz miniaturki
                string ThumbnailID = Utility.Crypto.MD5.ComputeMD5Hash(url);
                bool SecoundTime = false;
            TwoTimes:
                try
                {
                    //pobierz oryginal
                    Utility.Net.Web.DownloadRemoteFile(url, ImgFolderPath + Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_org.jpg");
                }
                catch (Exception e)
                {
                    if (!SecoundTime)
                    {
                        SecoundTime = true;
                        goto TwoTimes;
                    }
                    return false;
                }

                //zapisz w roznych rozmiarach
                //270x185 - max
                try
                {
                    Bitmap maxDpiTh = Utility.Bitmaps.Resize(new Bitmap(ImgFolderPath + Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_org.jpg"),
                                                           270, 185);
                    Utility.Bitmaps.SaveBitmapToFile(maxDpiTh, ImgFolderPath + Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_" + ThumbnailSizes.HighDpi.ToString() + ".jpg", 90);
                    //teraz mniejszy
                    Utility.Bitmaps.SaveBitmapToFile(Utility.Bitmaps.Resize(maxDpiTh,
                                                           200, 137), ImgFolderPath + Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_" + ThumbnailSizes.MediumDpi.ToString() + ".jpg", 90);
                    Utility.Bitmaps.SaveBitmapToFile(Utility.Bitmaps.Resize(maxDpiTh,
                                                           120, 82), ImgFolderPath + Utility.IO.FileSystem.GeneratePathBaseOnString(ThumbnailID) + "_" + ThumbnailSizes.LowDpi.ToString() + ".jpg", 90);
                }
                catch
                { }

                return true;
            }
            return false;
        }


        public enum ThumbnailSizes
        {
            LowDpi,
            MediumDpi,
            HighDpi
        }

    }
}